package org.test

import org.tests.IdealPriceStrategy

class PriceService {
	
	IdealPriceStrategy idealPriceStrategy;

    def getLowestPrice(Product product) {
		if (product.prices == null || product.prices.empty){
			return 0
		}
		PriceAssessment.createCriteria().get {
			eq("product", product)
			projections {
				min "price"
			}
		} as BigDecimal
    }
	
	def getHighestPrice(Product product) {
		if (product.prices == null || product.prices.empty){
			return 0
		}
		PriceAssessment.createCriteria().get {
			eq("product", product)
			projections {
				max "price"
			}
		} as BigDecimal
	}

	def getAveragePrice(Product product) {
		if (product.prices == null || product.prices.empty){
			return 0
		}
		PriceAssessment.createCriteria().get {
			eq("product", product)
			projections {
				avg "price"
			}
		} as BigDecimal
	}
		
	def getIdealPrice(Product product) {
		if (product.prices == null || product.prices.empty){
			return 0
		}
		idealPriceStrategy.getIdealPrice(product.prices)
	}
	
	def getNumberOfPrices(Product product) {
		if (product.prices == null || product.prices.empty){
			return 0
		}
		PriceAssessment.createCriteria().get {
			eq("product", product)
			projections {
				rowCount()
			}
		} as Integer
	}
	
	
}
